// SetPole.cpp : implementation file

#include "stdafx.h"
#include "Virtual Pole.h"
#include "SetPole.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif

/////////////////////////////////////////////////////////////////////////////
// CSetPole dialog


CSetPole::CSetPole(CWnd* pParent /*=NULL*/)
	: CDialog(CSetPole::IDD, pParent)
{
	//{{AFX_DATA_INIT(CSetPole)
	m_iVPMode = 0;
	m_Count = 0;
	m_bNoPole = FALSE;
	//}}AFX_DATA_INIT
}


void CSetPole::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CSetPole)
	DDX_Control(pDX, IDC_ADD_LIST_BTN, m_butAdd);
	DDX_Control(pDX, IDC_CLR_LIST, m_butClear);
	DDX_Control(pDX, IDC_REP_LIST_BTN, m_butReplace);
	DDX_Control(pDX, IDC_BUTTON2, m_butDelete);
	DDX_Control(pDX, IDC_POLE_LIST, m_lsbPoles);
	DDX_Text(pDX, IDC_POLE_EDIT_XPOS, m_lPole_XPos);
	DDV_MinMaxLong(pDX, m_lPole_XPos, 1, 1280);
	DDX_Text(pDX, IDC_POLE_EDIT_YPOS, m_lPole_YPos);
	DDV_MinMaxLong(pDX, m_lPole_YPos, 1, 1024);
	DDX_Text(pDX, IDC_POLE_EDIT_WIDTH, m_lPole_Width);
	DDV_MinMaxLong(pDX, m_lPole_Width, 1, 1000);
	DDX_Text(pDX, IDC_POLE_EDIT_HEIGHT, m_lPole_Height);
	DDV_MinMaxLong(pDX, m_lPole_Height, 1, 1000);
	DDX_Check(pDX, IDC_CHECK_NO_POLE, m_bNoPole);
	//}}AFX_DATA_MAP
}


BEGIN_MESSAGE_MAP(CSetPole, CDialog)
	//{{AFX_MSG_MAP(CSetPole)
	ON_BN_CLICKED(IDC_POLE_EDIT_BTN, OnPoleEditBtn)
	ON_BN_CLICKED(IDC_ADD_LIST_BTN, OnAddListBtn)
	ON_EN_CHANGE(IDC_POLE_EDIT_YPOS, OnChangePoleParam)
	ON_LBN_SELCHANGE(IDC_POLE_LIST, OnSelectionChangePoleList)
	ON_BN_CLICKED(IDC_BUTTON2, OnBtnDelete)
	ON_BN_CLICKED(IDC_CLR_LIST, OnClearList)
	ON_BN_CLICKED(IDC_REP_LIST_BTN, OnReplaceListBtn)
	ON_LBN_DBLCLK(IDC_POLE_LIST, OnDblclkPoleList)
	ON_EN_CHANGE(IDC_POLE_EDIT_XPOS, OnChangePoleParam)
	ON_EN_CHANGE(IDC_POLE_EDIT_WIDTH, OnChangePoleParam)
	ON_EN_CHANGE(IDC_POLE_EDIT_HEIGHT, OnChangePoleParam)
	ON_BN_CLICKED(IDC_CHECK_NO_POLE, OnCheckNoPole)
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CSetPole message handlers

void CSetPole::OnPoleEditBtn() {
	// mode 1 - select pole on frame
	m_idx = -1;
	m_lsbPoles.SetCurSel(m_idx);
	m_iVPMode = 1;
	OnOK();
}

int CSetPole::DoModal() {
	return CDialog::DoModal();
}

void CSetPole::OnAddListBtn() {
	CString s;
	
	UpdateData(true);
	// Check that position and dimensions of virtual object are valid
	if (m_lPole_XPos > 1200 || m_lPole_XPos < 1 
		|| m_lPole_YPos > 1200 || m_lPole_YPos < 1 
		|| m_lPole_Width < 1 || m_lPole_Height < 1) {
		AfxMessageBox("Pole parameters are invalid.\nUpdate parmeters and try again.", MB_ICONINFORMATION | MB_OK);
		return;
	}
	
	// create a string from all the data
	s.Format(" %7.3d %16.3d %13.3d %10.3d", m_lPole_XPos, m_lPole_YPos, m_lPole_Width, m_lPole_Height);
	
	// add the string
	int rows = m_lsbPoles.GetCount(); // items in list
	if (rows >= 10) { // allow max 10 items in list
		AfxMessageBox("Maximum 10 virtual objects can be configured.\nTo add new objects, you must first remove existing ones.", MB_ICONINFORMATION | MB_OK);
		return;
	}
	m_idx = m_lsbPoles.AddString(s);

	// if there is no current selection current selection to no selection
	if (m_idx == -1) {
		// Disable the delete & replace buttons
		m_butDelete.EnableWindow(false);
		m_butReplace.EnableWindow(false);
	} else {
		m_lsbPoles.SetCurSel(m_idx);
		m_butClear.EnableWindow(true);
	}
}

BOOL CSetPole::OnInitDialog() {
	CDialog::OnInitDialog();

	// Recall window position
	if (m_lpwndpl.rcNormalPosition.bottom > 0)
		SetWindowPlacement(&m_lpwndpl);

	// Put some default values in the text boxes only if needed
	if(m_iVPMode != 1) {
		m_lPole_XPos	= 1;
		m_lPole_YPos	= 1;
		m_lPole_Height	= 1;
		m_lPole_Width	= 1;
	}
	
	// If we need to load data then do so
	if (m_Count>0) {
		for (int i=0;i<m_Count;i++) {
			m_lsbPoles.AddString(*(m_pStrings+i));	
		}

		m_butClear.EnableWindow(true);
		
		// If we need to select an item from the list
		if (m_idx >= 0) {
			m_lsbPoles.GetText(m_idx,m_csCurrentString);
			m_lPole_XPos	= atol(m_csCurrentString.Mid(5,3));
			m_lPole_YPos	= atol(m_csCurrentString.Mid(22,3));
			m_lPole_Width	= atol(m_csCurrentString.Mid(36,3));
			m_lPole_Height	= atol(m_csCurrentString.Mid(47,3));
			m_lsbPoles.SetCurSel(m_idx);
			m_butDelete.EnableWindow(true);
			m_butReplace.EnableWindow(true);
		}

		// if we need to reposition the window
		if (m_iVPMode == 2) {
			m_idx = m_lsbPoles.SelectString(-1,m_csCurrentString);
			m_butDelete.EnableWindow(true);
			m_butReplace.EnableWindow(true);
		}
	}

	m_bNoPole =! m_bNoPole;
	OnCheckNoPole();
	UpdateData(false);
	
	// set mode to 0 - no mode, everything is normal
	m_iVPMode = 0;
		
	return TRUE;  // return TRUE unless you set the focus to a control
	              // EXCEPTION: OCX Property Pages should return FALSE
}

void CSetPole::OnChangePoleParam() {
	UpdateData(true);
}

void CSetPole::OnSelectionChangePoleList() {
	m_idx = m_lsbPoles.GetCurSel();
	
	// no selection - disable the delete & replace buttons button
	if (m_idx < 0) {		
		m_butDelete.EnableWindow(false);
		m_butReplace.EnableWindow(false);
		return;
	}
	
	m_butDelete.EnableWindow(true);
	m_butReplace.EnableWindow(true);
}

void CSetPole::OnBtnDelete() {
	// deletes the selected string
	m_lsbPoles.DeleteString(m_idx);

	// set the current selection to no selection
	m_idx = -1;
	
	// set the list box to select nothing
	m_lsbPoles.SetCurSel(m_idx);
	
	// disable the delete & replace buttons
	m_butDelete.EnableWindow(false);
	m_butReplace.EnableWindow(false);

	// if we've deleted the last item on the list then disable the clear list button
	if (m_lsbPoles.GetCount() < 1)
		m_butClear.EnableWindow(false);
}

void CSetPole::OnOK() {
	//check how many items are on the list
	m_Count = m_lsbPoles.GetCount();
	
	// Remember window position
	GetWindowPlacement(&m_lpwndpl);

	// if there is nothing on the list then just finish
	if (m_Count<1) {
		CDialog::OnOK();
		return;
	}

	CString s;
	
	// if there are poles on the list then let's clear the old list and make some room for the new poles
	delete [] m_pStrings;
	m_pStrings = NULL;
	m_pStrings = new CString[m_Count];

	// save the strings	and the randomly selected pole if needed
	for (int i = 0; i < m_Count; i++) {
		//save strings
		m_lsbPoles.GetText(i,s);
		*(m_pStrings+i) = s;
	}

	if (m_idx >- 1) {
		m_lsbPoles.GetText(m_idx,m_csCurrentString);
		m_lPole_XPos	= atol(m_csCurrentString.Mid(5,3));
		m_lPole_YPos	= atol(m_csCurrentString.Mid(22,3));
		m_lPole_Width	= atol(m_csCurrentString.Mid(36,3));
		m_lPole_Height	= atol(m_csCurrentString.Mid(47,3));
		m_lsbPoles.SetCurSel(m_idx);
		
		// Save all object coordinates
		for (int i=0; i<10; i++) {
			if (i < m_Count) {
				m_lsbPoles.GetText(i, m_csCurrentString);
				m_lPolesX[i]		= atol(m_csCurrentString.Mid(5,3));
				m_lPolesY[i]		= atol(m_csCurrentString.Mid(22,3));
				m_lPolesWidth[i]	= atol(m_csCurrentString.Mid(36,3));
				m_lPolesHeight[i]	= atol(m_csCurrentString.Mid(47,3));
			} else {
				m_lPolesX[i]		= 0;
				m_lPolesY[i]		= 0;
				m_lPolesWidth[i]	= 0;
				m_lPolesHeight[i]	= 0;			
			}
		}

	}

	CDialog::OnOK();
	return;
}

void CSetPole::OnClearList() {
	m_lsbPoles.ResetContent();
	// set the current selection to no selection
	m_idx = -1;
	
	// disable the delete replace and clear buttons
	m_butDelete.EnableWindow(false);
	m_butReplace.EnableWindow(false);
	m_butClear.EnableWindow(false);
}

void CSetPole::OnReplaceListBtn() {
	CString s;

	UpdateData(true);
	//checks data to be vaild
	if (m_lPole_XPos > 1200 || m_lPole_XPos < 1 || m_lPole_YPos > 1200 || m_lPole_YPos < 1 || m_lPole_Width > 100 || m_lPole_Width < 1 || m_lPole_Height > 100 || m_lPole_Height < 1)
	{
		AfxMessageBox("Pole parameters are illegal!\nUpdate parmeters & try again.", MB_ICONINFORMATION | MB_OK);
		return;
	}
	
	// create a string from all the data
	s.Format(" %7.3d %16.3d %13.3d %10.3d",m_lPole_XPos, m_lPole_YPos, m_lPole_Width, m_lPole_Height);
	
	// add the new string
	m_lsbPoles.InsertString(m_idx,s);
	
	// remove the old one
	m_idx++;
	OnBtnDelete();
	m_idx = m_lsbPoles.SelectString(-1,s);
	m_butDelete.EnableWindow(true);
	m_butReplace.EnableWindow(true);
}

void CSetPole::OnDblclkPoleList() {
	m_lsbPoles.GetText(m_idx,m_csCurrentString);
	m_lPole_XPos	= atol(m_csCurrentString.Mid(5,3));
	m_lPole_YPos	= atol(m_csCurrentString.Mid(22,3));
	m_lPole_Width	= atol(m_csCurrentString.Mid(36,3));
	m_lPole_Height	= atol(m_csCurrentString.Mid(47,3));
	UpdateData(false);
	
	// mode 2 - just update the view and return here
	m_iVPMode = 2;
	OnOK();
}

void CSetPole::OnCheckNoPole() {
	m_bNoPole =! m_bNoPole;
	if (m_bNoPole) {
		m_butDelete.EnableWindow(false);
		m_butReplace.EnableWindow(false);
		m_butClear.EnableWindow(false);
		m_lsbPoles.EnableWindow(false);
		m_butAdd.EnableWindow(false);
	} else {
		m_lsbPoles.EnableWindow(true);
		m_butAdd.EnableWindow(true);
		if (m_idx >= 0) {
			m_butDelete.EnableWindow(true);
			m_butReplace.EnableWindow(true);
		}
		if (m_lsbPoles.GetCount() > 0)
			m_butClear.EnableWindow(true);
	}
}
